package com.jrx.anytxn.atom.service.impl;

import com.jrx.anytxn.atom.bean.CustBatchSaveBean;
import com.jrx.anytxn.atom.service.BaseBatchAccoutHandle;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.customer.entity.CmCustomerFirstLevel;
import com.jrx.anytxn.investor.entity.*;
import com.jrx.anytxn.investor.mapper.ext.*;
import com.jrx.anytxn.param.entity.PrOrganizationTable;
import com.jrx.anytxn.param.service.system.IOrganizationTableService;
import com.jrx.anytxn.transaction.constant.TransBizConstant;
import com.jrx.anytxn.transaction.entity.*;
import com.jrx.anytxn.transaction.mapper.ext.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: anytxn-cf-parent
 * @description: 客户批量-批量初始化
 * @author: xiaoyu.jing
 * @date: 2020-09-02 15:54
 **/
@Order(0)
@Service
public class InitBatchHandle extends BaseBatchAccoutHandle<CustBatchSaveBean> {

    Logger logger = LoggerFactory.getLogger(InitBatchHandle.class);

    @Autowired
    private ExtAmLoanAccountInfoMapper extAmLoanAccountInfoMapper;
    @Autowired
    private ExtAmLoanMainInfoMapper extAmLoanMainInfoMapper;
    @Autowired
    private ExtAmAccountMainInfoMapper extAmAccountMainInfoMapper;
    @Autowired
    private ExtAmRepaymentPlanInfoMapper extAmRepaymentPlanInfoMapper;
    @Autowired
    private ExtAmWaiveRepaymentPlanInfoMapper extAmWaiveRepaymentPlanInfoMapper;
    @Autowired
    private ExtAmInterestAccrualInfoMapper extAmInterestAccrualInfoMapper;
    @Autowired
    private ExtAmDelinquencyInfoMapper extAmDelinquencyInfoMapper;
    @Autowired
    private ExtAmLoanAccountInvestorInfoMapper extAmLoanAccountInvestorInfoMapper;
    @Autowired
    private ExtAmLoanMainInvestorInfoMapper extAmLoanMainInvestorInfoMapper;
    @Autowired
    private ExtAmAccountMainInvestorInfoMapper extAmAccountMainInvestorInfoMapper;
    @Autowired
    private ExtAmRepaymentPlanInvestorInfoMapper extAmRepaymentPlanInvestorInfoMapper;
    @Autowired
    private ExtAmInterestAccrualInvestorInfoMapper extAmInterestAccrualInvestorInfoMapper;
    @Autowired
    private ExtAmDelinquencyInvestorInfoMapper extAmDelinquencyInvestorInfoMapper;
    @Autowired
    private IOrganizationTableService organizationTableService;



    @Override
    public List<CustBatchSaveBean> handBatchAccout() throws TxnException {
        CustBatchSaveBean custBatchSaveBean = CustBatchHelper.getCustBatchLocal();
        CmCustomerFirstLevel firstLevel = custBatchSaveBean.getDealCust();
        String customerId = firstLevel.getCustomerId();
        String tenantId = firstLevel.getTenantId();
        PrOrganizationTable org = organizationTableService.findByOrId(firstLevel.getOrganizationId(),tenantId);
        firstLevel.setNextProcessingDate(org.getNextProcessingDate());
        CmCustomerFirstLevel updateCust = new CmCustomerFirstLevel();
        updateCust.setId(firstLevel.getId());
        updateCust.setCustomerId(firstLevel.getCustomerId());
        updateCust.setTenantId(firstLevel.getTenantId());
        updateCust.setNextProcessingDate(firstLevel.getNextProcessingDate());
        updateCust.setBlockCode(firstLevel.getBlockCode());//封锁码改造
        custBatchSaveBean.setUpdateCust(updateCust);


        List<AmLoanAccountInfo> dealLoanAcctList = extAmLoanAccountInfoMapper
                .selectByCustAndChannelAndStatus(customerId,null,tenantId, Arrays.asList(TransBizConstant.LOAN_STATUS_1,TransBizConstant.LOAN_STATUS_2));
        List<AmLoanMainInfo> dealLoanMainList = extAmLoanMainInfoMapper.selectByCusIdtAndTenantId(customerId,tenantId);
        List<AmRepaymentPlanInfo> dealPlanList = extAmRepaymentPlanInfoMapper.selectRepaymentPlanInfoListByLoanIdAndStatus(null
                ,new String[]{TransBizConstant.LOAN_PLAN_STATUS_0,TransBizConstant.LOAN_PLAN_STATUS_1,TransBizConstant.LOAN_PLAN_STATUS_2},customerId,tenantId);
        List<AmWaiveRepaymentPlanInfo> dealWaivePlanList = extAmWaiveRepaymentPlanInfoMapper.selectRepaymentPlanInfoListByLoanIdAndStatus(null
                ,new String[]{TransBizConstant.LOAN_PLAN_STATUS_0,TransBizConstant.LOAN_PLAN_STATUS_1,TransBizConstant.LOAN_PLAN_STATUS_2},customerId,tenantId);
        List<AmAccountMainInfo> dealAcctList = extAmAccountMainInfoMapper
                .selectByStatusAndType(null, new String[]{TransBizConstant.ACCOUNT_STATUS_1, TransBizConstant.ACCOUNT_STATUS_2}
                ,null,null,customerId,tenantId);
        List<AmInterestAccrualInfo> dealAccuAcctList = extAmInterestAccrualInfoMapper.selectByCust(customerId,tenantId, Collections.singletonList(TransBizConstant.ACCOUNT_STATUS_1));
        List<AmDelinquencyInfo> dealDelqList = extAmDelinquencyInfoMapper.selectDelqInfoByLoanId(null,customerId,tenantId,Collections.singletonList(TransBizConstant.ACCOUNT_STATUS_1));

        List<AmLoanAccountInvestorInfo> dealInvestorLoanAcctList = extAmLoanAccountInvestorInfoMapper
                .findByCust(customerId,tenantId, Arrays.asList(TransBizConstant.LOAN_STATUS_1,TransBizConstant.LOAN_STATUS_2));
        List<AmLoanMainInvestorInfo> dealInvestorLoanMainList = extAmLoanMainInvestorInfoMapper.findByCust(customerId,tenantId);
        List<AmRepaymentPlanInvestorInfo> dealInvestorPlanList = extAmRepaymentPlanInvestorInfoMapper.findByCust(
                new String[]{TransBizConstant.LOAN_PLAN_STATUS_0,TransBizConstant.LOAN_PLAN_STATUS_1,TransBizConstant.LOAN_PLAN_STATUS_2},customerId,tenantId);
        List<AmAccountMainInvestorInfo> dealInvestorAcctList = extAmAccountMainInvestorInfoMapper.findByCust(customerId,tenantId,Arrays.asList(TransBizConstant.ACCOUNT_STATUS_1, TransBizConstant.ACCOUNT_STATUS_2));
        List<AmInterestAccrualInvestorInfo> dealInvestorAccuAcctList = extAmInterestAccrualInvestorInfoMapper.findByCust(customerId,tenantId, Collections.singletonList(TransBizConstant.ACCOUNT_STATUS_1));
        List<AmDelinquencyInvestorInfo> dealInvestorDelqList = extAmDelinquencyInvestorInfoMapper.findByCust(customerId,tenantId,TransBizConstant.ACCOUNT_STATUS_1);
        custBatchSaveBean.setDealLoanMainList(dealLoanMainList);
        custBatchSaveBean.setDealLoanAccountList(dealLoanAcctList);
        custBatchSaveBean.setDealPlanList(dealPlanList);
        custBatchSaveBean.setDealWaivePlanList(dealWaivePlanList);
        custBatchSaveBean.setDealAcctList(dealAcctList);
        custBatchSaveBean.setDealAccuAcctList(dealAccuAcctList);
        custBatchSaveBean.setDealDelqAcctList(dealDelqList);
        custBatchSaveBean.setDealInvestorLoanMainList(dealInvestorLoanMainList);
        custBatchSaveBean.setDealInvestorLoanAccountList(dealInvestorLoanAcctList);
        custBatchSaveBean.setDealInvestorPlanList(dealInvestorPlanList);
        custBatchSaveBean.setDealInvestorAcctList(dealInvestorAcctList);
        custBatchSaveBean.setDealInvestorAccuAcctList(dealInvestorAccuAcctList);
        custBatchSaveBean.setDealInvestorDelqAcctList(dealInvestorDelqList);
        logger.info("InitBatchHandle用户:{},需要处理的订单：{}", customerId, dealLoanAcctList.stream().map(AmLoanAccountInfo::getLoanId).collect(Collectors.joining(",")));
        return null;
    }
}
